package com.orcamento.util;

import com.orcamento.entity.enums.Month;

public class Period {

	private Month month;
	private Integer year;

	public Period(Integer year, Integer month) {
		this.year = year;
		if( month != null ) {
			this.month = Month.findById(month);
		}
	}

	public Period previous() {
		if( Month.JANUARY.equals( month ) ) {
			return new Period(year-1, month.previous().id);
		}
		
		return new Period(year, month.previous().id);
	}
	
	public Period next() {
		if( Month.DEZEMBER.equals( month ) ) {
			return new Period( year+1, month.next().id );
		}
		
		return new Period( year, month.next().id );
	}

	public Integer getYear() {
		return year;
	}
	
	public Month getMonth() {
		return month;
	}
	
	@Override
	public boolean equals(Object object) {
		if( object == null ) {
			return false;
		}
		if( object.getClass() != getClass() ) {
			return false;
		}
		
		Period period = (Period) object;
		if( month == null || year == null || period.month == null || period.year == null ) {
			return false;
		}
		
		return month.equals( period.month ) && year.equals( period.year );
	}
	
	@Override
	public String toString() {
		return year + "-" + month.getIdComplete();
	}
	
}
